<?php

namespace App\Http\Controllers\Web\Buyer;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Models\OrderProducts;
use App\Repository\Models\Orders;
use App\Repository\Repositories\Interfaces\AddressRepository;
use App\Repository\Repositories\Interfaces\CartsRepository;
use App\Repository\Repositories\Interfaces\CompanyRepository;
use App\Repository\Repositories\Interfaces\InvoicesRepository;
use App\Repository\Repositories\Interfaces\OrderExpressRepository;
use App\Repository\Repositories\Interfaces\OrderProductCommentRepository;
use App\Repository\Repositories\Interfaces\OrderProductsRepository;
use App\Repository\Repositories\Interfaces\OrdersRepository;
use App\Repository\Repositories\Interfaces\ProductSkusRepository;
use App\Repository\Repositories\Interfaces\ProductsRepository;
use App\Repository\Repositories\Interfaces\ShopRepository;
use App\Repository\Repositories\Interfaces\UserInvoiceRepository;
use App\Repository\Repositories\ProductAttributesRepositoryEloquent;
use App\Services\Api\KuaiDi100;
use Illuminate\Http\Request;

/**
 * 商品控制器
 *
 * @package App\Http\Controllers\Backend
 */
class ProductAttributesController extends BaseController
{
    /**
     * @var ProductSkusRepository
     */
    protected $product_attr_repo;

    public function __construct(ProductAttributesRepositoryEloquent $product_attr_repo)
    {
        parent::__construct();
        $this->shop_id = array_get($this->user, 'shop_id');
        $this->product_attr_repo = $product_attr_repo;;
    }

    /**
     * 直接下单商铺-商品列表
     * @param Request $request
     * @param ShopRepository $shop_repo
     * @param $id
     * @return \Illuminate\Support\Collection
     */
    public function show(Request $request, ShopRepository $shop_repo){
        $this->validate($request, [
            'ids' => 'required', //例如：[223=>2,322=>1]，key为sku_id，value为购买数量
        ]);
        $data=$request->all();
        $sku_num = $request->get('ids');

        $sku = $this->product_attr_repo->with(['product'])->scopeQuery(function($sql) use($sku_num){
            return $sql->whereIn('id',$sku_num);
        })->all();
        $sku=$sku['data'];
        //存在自己店铺产品
        foreach($sku as $k=>$v){
            if($this->shop_id==$v['product']['shop_id']){
                return $this->error(ErrorConst::PRODUCT_USER_MSG, ErrorConst::PRODUCT_USER);
            }
        }

        $shop = $shop_repo->find(array_get($sku[0], 'product.shop_id'), ['sp_id', 'sp_name']);

        $shop['data']['attris'][] = $sku;
        $shops[] = $shop['data'];

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $shops);
    }
}
